package com.pinyoumei.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pinyoumei.base.TaotaoResult;
import com.pinyoumei.domain.Cart;
import com.pinyoumei.pojo.TbOrder;
import com.pinyoumei.utils.CookieUtils;
import com.pinyoumei.utils.JsonUtils;
import com.pinyoumei.web.fegin.CartServiceFegin;
import com.pinyoumei.web.fegin.OrderServiceFegin;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/cart")
public class CartController {
	
	@Autowired
	private CartServiceFegin cartServiceFegin;
	
	@Autowired
	private  OrderServiceFegin orderServiceFegin;
	
	/**
	 * localhost:8080/cart/create?itemId=917770&num=1
	 * @param request
	 * @param response
	 * @return
	 */
    @ResponseBody
	@RequestMapping("/create")
	public TaotaoResult addGoodsToCartList(HttpServletRequest request,HttpServletResponse response){
		Long itemId=Long.parseLong(request.getParameter("itemId"));
		Integer num = Integer.valueOf(request.getParameter("num"));
		log.info("接收参数itemId:{},num:{}",itemId,num);
		try {
			//1. 从cookie中提取购物车
			String cartListString=getCartListFromCookie(request);
			//2. 调用方法操作购物车
			cartListString=cartServiceFegin.addGoodsToCartList(cartListString,itemId,num);
			log.info("返回参数cartListString:{}",cartListString);
			//3. 将新的购物车放入cookie 一周
			CookieUtils.setCookie(request, response,"CART", cartListString, 3600*24*7,"UTF-8");
		    return TaotaoResult.build(200, "存入购物车成功");
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(201, "存入购物车失败");
		}
		
	}
	
    /**
     * localhost:8080/cart/getCartList
     * @param request
     * @return
     */
	@ResponseBody
	@RequestMapping("/getCartList")
	public String getCartListFromCookie(HttpServletRequest request){
		String cartListString=CookieUtils.getCookieValue(request,"CART","UTF-8");
		if(cartListString==null||cartListString.equals("")){
			cartListString="[]";
		}
		return cartListString;
	}
	
	/**
	 * 跳转到我的购物车页面
	 * 涉及 单点登录系统开发(SSO),拦截器,消息系统重新设计与会员系统拆分  
	 * 前期用户数据先写死,打通支付流程后再完善
	 * @return
	 */
	@RequestMapping("/mycart")
	public String myCart(HttpServletRequest request,HttpServletResponse response){
		String cartListString=getCartListFromCookie(request);
		String userId=8+"";
		//购物车写入redis
		cartServiceFegin.writeCartList2Redis(cartListString, userId);
		//清除cookie购物车
		CookieUtils.deleteCookie(request, response, "CART");
		List<Cart> cartList=JsonUtils.jsonToList(cartListString, Cart.class);
		request.setAttribute("cartList",cartList);
		return "cart/mycart";
	}
	
	/**
	 * 跳转到订单页
	 * @return
	 */
	@RequestMapping("/toOrder")
	public String toOrder(HttpServletRequest request){
		//从Redis中获取购物车信息
		String cartListString=orderServiceFegin.getCartList4userId("8");
		if(null==cartListString||"".equals(cartListString)){
			cartListString="[]";
		}
		List<Cart> cartList=JsonUtils.jsonToList(cartListString, Cart.class);
		request.setAttribute("cartList",cartList);
		return "order/orderInfo";
	}
	
	@RequestMapping("/add/order")
	public String addOrder(TbOrder order){
		//feign创建订单接口
		String taotaoResult=orderServiceFegin.add(order);
		log.info("调用创建订单接口返回结果:{}",taotaoResult);
		TaotaoResult taotao=JsonUtils.jsonToPojo(taotaoResult,TaotaoResult.class);
		if(taotao.getStatus()==200){
			return "order/success";
		}
		return "order/fail";
	}
}
